//
//  commentViewController.m
//  weibo3
//
//  Created by Tian Wu on 11-4-19.
//  Copyright 2011年 nfsysu. All rights reserved.
//

#import "commentViewController.h"


@implementation commentViewController

@synthesize commented , comments;

- (NSString *)windowNibName
{
	return @"CommentView";
}


- (void) reloadTable{
    [progress startAnimation:nil];
    
    self.comments = [_rootViewController getComments:[self.commented aid] count:20 page:1];
    
    [listView setDataSource:self];
    [listView setDelegate:self];
    [listView reloadData];
    
    [progress stopAnimation:nil];
    
}


- (BOOL)edit:(Status *)startingValues from:(id)sender
{
        
	[NSApp beginSheet:[self window] modalForWindow:[(NSWindowController *)sender window] modalDelegate:nil didEndSelector:nil contextInfo:nil];
    
    
    self.commented = startingValues;

    [self reloadTable];
    
    
    
    
	[NSApp runModalForWindow:[self window]];
    
	// sheet is up here...
    
	[NSApp endSheet:[self window]];
	[[self window] orderOut:self];
    
    return YES;
}


- (IBAction) sendComment:(id) sender{
    
    NSString *status = [text stringValue];
    
    if ([status length] > 140) {
        NSAlert *alert = [NSAlert alertWithMessageText:@"字数太多了" defaultButton:@"好的" alternateButton:nil otherButton:nil informativeTextWithFormat:@"最多可以输入140字，而您已经输入了%d字噢！",[[text stringValue] length]];
        [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:NULL];
    }
    else if([status length] < 1){
        NSAlert *alert = [NSAlert alertWithMessageText:@"请输入文字" defaultButton:@"好的" alternateButton:nil otherButton:nil informativeTextWithFormat:@"请在白色区域中输入您想要发送的微博！"];
        [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:NULL];
    }
    else{
                
        if ([withRepostButton state] == NSOnState) {
            [_rootViewController statusRepost:[self.commented aid] status:status isComment:3];
        }
        else{
            [_rootViewController comment:[self.commented aid] commentString:status];
        }
        
        [text setStringValue:@""];
        
        [self reloadTable];
        [self reloadTable];
        
    }
        
}
    


- (IBAction) cancel:(id) sender{
    [NSApp stopModal];
}






- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView{
    if ([self.comments count] >20) {
        return 20;
    }
    return [self.comments count];
    
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    
    if (aTableColumn == [[aTableView tableColumns] objectAtIndex:0]) {
        NSImage *temp = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:[(User *)[[self.comments objectAtIndex:rowIndex] user] profile_image_url]]];
        [temp autorelease];
        return temp;
    }
    else{
        if (rowIndex >= [self.comments count]) {
            return @"";
        }
        return [NSString stringWithFormat:@"@%@:%@",[(User *)[[self.comments objectAtIndex:rowIndex] user] name],[[self.comments objectAtIndex:rowIndex] text]];
    }
    
}

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    
    
}


- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        
    }
    
    return self;
}


- (void)dealloc
{
    [super dealloc];
    
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    [withRepostButton setState:NSOffState];
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

@end
